<?php

/**
 * This is the model class for table "tbl_province".
 *
 * The followings are the available columns in table 'tbl_province':
 * @property integer $PROVINCE_ID
 * @property string $PROVINCE_CODE
 * @property string $PROVINCE_NAME
 * @property integer $GEO_ID
 *
 * The followings are the available model relations:
 * @property TblGeography $gEO
 */
class ProvinceModel extends CActiveRecord {

    public $name_geo;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ProvinceModel the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_province';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('PROVINCE_CODE, PROVINCE_NAME', 'required'),
            array('GEO_ID', 'numerical', 'integerOnly' => true),
            array('PROVINCE_CODE', 'length', 'max' => 2),
            array('PROVINCE_NAME', 'length', 'max' => 150),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('PROVINCE_ID, PROVINCE_CODE, PROVINCE_NAME, GEO_ID', 'safe', 'on' => 'search'),
            array('name_geo', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'gEO' => array(self::BELONGS_TO, 'GeographyModel', 'GEO_ID'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'PROVINCE_ID' => 'Province',
            'PROVINCE_CODE' => 'Province Code',
            'PROVINCE_NAME' => 'Province Name',
            'GEO_ID' => 'Geo',
            'name_geo' => 'GEO NAME',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('PROVINCE_ID', $this->PROVINCE_ID);
        $criteria->compare('PROVINCE_CODE', $this->PROVINCE_CODE, true);
        $criteria->compare('PROVINCE_NAME', $this->PROVINCE_NAME, true);
        $criteria->compare('GEO_ID', $this->GEO_ID);

        //  if($this->name_geo)
        //{
        $criteria->together = true;
        $criteria->with = array('gEO');
        $criteria->compare('gEO.GEO_NAME', $this->name_geo, true);
        // }
        // Create a custom sort
        $sort = new CSort;
        $sort->attributes = array(
            // For each relational attribute, create a 'virtual attribute' using the public variable name
            'name_geo' => array(
                'asc' => 'gEO.GEO_NAME',
                'desc' => 'gEO.GEO_NAME DESC',
                'label' => 'GEO NAME',
            ),
            '*',
        );

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => $sort,
                ));
    }

}